package com.imooc.service.impl;

import com.github.tobato.fastdfs.domain.fdfs.StorePath;
import com.github.tobato.fastdfs.service.FastFileStorageClient;
import com.imooc.service.FdfsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.nio.charset.StandardCharsets;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;

/**
 * @author: 顾志杰
 */
@Service
public class FdfsServiceImpl implements FdfsService {

    @Autowired
    private FastFileStorageClient fastFileStorageClient;


    @Override
    public String upload(MultipartFile file) throws Exception {
        /*文件名*/
        String upFileName = file.getOriginalFilename();
        /*文件后缀名*/
        assert upFileName != null;
        String fileExtName = upFileName.substring(upFileName.lastIndexOf(".")+1);

        StorePath storePath = fastFileStorageClient.uploadFile(
                /*文件流*/
                file.getInputStream(),
                /*文件大小*/
                file.getSize(),
                fileExtName,
                null);
        String pash = storePath.getFullPath();
        return pash;
    }


    private static String md5(byte[] source) throws NoSuchAlgorithmException {
        char[] hexDigits = new char[]{'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f'};
        MessageDigest md = MessageDigest.getInstance("MD5");
        md.update(source);
        byte[] tmp = md.digest();
        char[] str = new char[32];
        int k = 0;

        for(int i = 0; i < 16; ++i) {
            str[k++] = hexDigits[tmp[i] >>> 4 & 15];
            str[k++] = hexDigits[tmp[i] & 15];
        }

        return new String(str);
    }


    public  String getToken(String remote_filename, int ts, String secret_key) throws  Exception {
        byte[] bsFilename = remote_filename.getBytes(StandardCharsets.UTF_8);
        byte[] bsKey = secret_key.getBytes(StandardCharsets.UTF_8);
        byte[] bsTimestamp = (new Integer(ts)).toString().getBytes(StandardCharsets.UTF_8);
        byte[] buff = new byte[bsFilename.length + bsKey.length + bsTimestamp.length];
        System.arraycopy(bsFilename, 0, buff, 0, bsFilename.length);
        System.arraycopy(bsKey, 0, buff, bsFilename.length, bsKey.length);
        System.arraycopy(bsTimestamp, 0, buff, bsFilename.length + bsKey.length, bsTimestamp.length);
        return md5(buff);
    }


    @Override
    public String getToken() throws Exception{
        int ts = (int) (System.currentTimeMillis() / 1000);
        String remoteFilename = "M00/00/07/wKgYy2BQcaaAKTvmAAC0SsNiwOU799.jpg";
        System.out.println("lts="+ts);
        String token = this.getToken(remoteFilename, ts, "1234567890");
        System.out.println("token="+token);
        System.out.println("http://192.168.24.203:8090/group1/"+remoteFilename+"?token="+token+"&ts="+ts);
        return "http://192.168.24.203:8090/group1/"+remoteFilename+"?token="+token+"&ts="+ts;
    }
}
